//
//  GDPMeituanRefreshView.swift
//  simpleWeibo
//
//  Created by sunmumu on 2019/1/16.
//  Copyright © 2019 孙林. All rights reserved.
//

import UIKit

class GDPMeituanRefreshView: GDPRefreshview {

    @IBOutlet weak var buildingIconView: UIImageView!
    
    @IBOutlet weak var earthIconView: UIImageView!
    
    @IBOutlet weak var kangaroonIconView: UIImageView!
    
    override var parentViewHeight: CGFloat {
        didSet {
            
            if parentViewHeight < 23 {
                return
            }
            // 23 - 126
            //0.2 - 1
            //高度差 / 最大高度差
            // 23 == 1
            // 126 == 0.2
            var scale : CGFloat = 0
            if parentViewHeight > 126 {
                scale = 1
            } else {
                scale = 1 - ((126 - parentViewHeight) / (126 - 23))
            }
            kangaroonIconView.transform = CGAffineTransform(scaleX: scale, y: scale)
        }
    }
    
    override func awakeFromNib() {
        //1房子
        let buildingIconImage1 = UIImage.init(named: "icon_building_loading_1")
        let buildingIconImage2 = UIImage.init(named: "icon_building_loading_2")
        buildingIconView.image = UIImage.animatedImage(with: [buildingIconImage1 ?? UIImage.init(), buildingIconImage2 ?? UIImage.init()], duration: 0.25)
        
        //2地球
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = -2 * Double.pi
        animation.repeatCount = MAXFLOAT
        animation.duration = 3
        animation.isRemovedOnCompletion = false
        
        earthIconView.layer.add(animation, forKey: nil)
        
        //3袋鼠
        let kangaroonImage1 = UIImage.init(named: "icon_small_kangaroo_loading_1")
        let kangaroonImage2 = UIImage.init(named: "icon_small_kangaroo_loading_2")
        kangaroonIconView.image = UIImage.animatedImage(with: [kangaroonImage1 ?? UIImage.init(), kangaroonImage2 ?? UIImage.init()], duration: 0.25)
        
        kangaroonIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        let x = self.bounds.width * 0.5
        let y = self.bounds.height - 23
        kangaroonIconView.center = CGPoint(x: x, y: y)
        kangaroonIconView.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
    }
    
}
